using System;

using Impact.Infrastructure.Exceptions;
using Impact.Infrastructure.State;
using Impact.Model.Objects;
using Impact.Model.Service;

using StructureMap;

namespace Impact.Controllers
{
    /// <summary>
    /// Controller for the view to edit plugins
    /// </summary>
    public class EditPluginController
    {

        private ImpactPage page;
        private IPluginService service;

        /// <summary>
        /// default constructor
        /// </summary>
        public EditPluginController(int pageId)
        {
            Check.Require(pageId > 0, "Must provide a valid pageID");

            this.service = ObjectFactory.GetInstance<IPluginService>();

            IPageService pageService = ObjectFactory.GetInstance<IPageService>();
            this.page = pageService.FetchByID(pageId);
            Check.Require(ImpactPage.IsValid(page), "Page not found");
        }

        /// <summary>
        /// deletes the plugin with the given registrationId
        /// </summary>
        public void DeletePlugin(int registrationId)
        {
            service.Remove(registrationId);
        }

        /// <summary>
        /// adds the given plugin to the given page
        /// </summary>
        public void AddPlugin(int pageId, Guid pluginId)
        {
            service.Add(pageId, pluginId, Transaction.Current.UserId.Value);
        }

        /// <summary>
        /// increments the seequence to move the plugin down (vertically) on the page
        /// </summary>
        /// <param name="registrationId"></param>
        public void IncreaseSequence(int registrationId)
        {
            PagePlugin plugin = service.FetchByID(registrationId);
            plugin.Sequence += 11;
            service.Save(plugin, Transaction.Current.UserId.Value);
        }

        /// <summary>
        /// decrements the sequence to move the plugin up (vertically) on the page
        /// </summary>
        /// <param name="registrationId"></param>
        public void DecreaseSequence(int registrationId)
        {
            PagePlugin plugin = service.FetchByID(registrationId);
            plugin.Sequence -= 11;
            service.Save(plugin, Transaction.Current.UserId.Value);
        }

        /// <summary>
        /// retrieve all the plugins for this page
        /// </summary>
        public PagePluginCollection GetPlugins()
        {
            IPluginService service = ObjectFactory.GetInstance<IPluginService>();
            return service.FetchByPage(page.PageId);
        }

        /// <summary>
        /// retrieve the string caption for this page
        /// </summary>
        public string GetPageCaption()
        {
            return page.Caption;
        }

        /// <summary>
        /// save the plugin
        /// </summary>
        public void Save(PagePlugin plugin)
        {
            service.Save(plugin, Transaction.Current.UserId.Value);
        }

    }
}
